<?php

abstract class CSite extends CModule
{
    public $siteName = 'Woosize';
    public $charset = 'utf-8';
    public $sourceLanguage = 'en_us';

    private $_id;
    private $_basePath;
    private $_runtimePath;
    private $_extensionPath;
    private $_globalState;
    private $_stateChanged;
    private $_ended = false;
    private $_language;
    private $_homeUrl;

    abstract public function processRequest();

    public function __construct($config=null)
    {
        ws::setSite($this);

        if(is_string($config))
            $config = require($config);
        if(isset($config['basePath']))
        {
            $this->setBasePath($config['basePath']);
            unset($config['basePath']);
        }
        else
            $this->setBasePath('protected');

        ws::setPathOfAlias('site', $this->getBasePath());
        ws::setPathOfAlias('webroot', dirname($_SERVER['SCRIPT_FILENAME']));
        ws::setPathOfAlias('ext', $this->getBasePath(). DS. 'extensions');

        $this->preinit();

        $this->initSystemHandlers();
        $this->registerCoreComponents();

        $this->configure($config);
        $this->attachBehaviors($this->behaviors);
        $this->preloadComponents();

        $this->init();
    }

    public function run()
    {
        if($this->hasEventHandler('onBeginRequest'))
            $this->onBeginRequest(new CEvent($this));

        $this->processRequest();

        if($this->hasEventHandler('onEndRequest'))
            $this->onEndRequest(new CEvent($this));
    }

    public function onBeginRequest($event)
    {
        $this->raiseEvent('onBeginRequest', $event);
    }

    public function onEndRequest($event)
    {
        if(!$this->_ended)
        {
            $this->_ended = true;
            $this->raiseEvent('onEndRequest', $event);
        }
    }

    public function onException($event)
    {
        $this->raiseEvent('onException', $event);
    }

    public function onError($event)
    {
        $this->raiseEvent('onError', $event);
    }

    public function getBasePath()
    {
        return $this->_basePath;
    }

    public function setBasePath($path)
    {
        if(($this->_basePaht = realpath($path)) === false || !is_dir($this->_basePath))
            throw new CException();
    }

    public function getTimeZone()
    {
        return date_default_timezone_get();
    }

    public function setTimeZone($value)
    {
        date_default_timezone_set($value);
    }

    public function configure($config)
    {
        if(is_array($config))
        {
            foreach($config as $key => $value)
                $this->$key = $value;
        }
    }
}
